package com.melody.renderview;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.melody.renderview.encoder.ViewEncoder;
import com.melody.renderview.view.RenderView;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    private static final int MSG_CHANGE = 10001;
    private RenderView mRenderView;
    private SurfaceView mSurfaceView;

    private ViewEncoder mEncoder;
    private String mFilePath;

    private boolean isStartRecordToMediaCodec;
    private boolean isStartRecordToSurfaceView;

    private Button mRenderToSurfaceViewBtn;
    private Button mRenderToMediaCodecBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initEncoder();
        initDefaultArgs();
    }

    private void initView(){
        Log.d("rendViewToOpenGL", "initView");
        mRenderView = (RenderView) findViewById(R.id.rw_record_view);
        mSurfaceView = (SurfaceView) findViewById(R.id.sv_surface_view);

        mRenderToSurfaceViewBtn = (Button) findViewById(R.id.btn_render_to_surfaceview);
        mRenderToSurfaceViewBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                if (isStartRecordToSurfaceView) {
                    mRenderView.setIsRenderToSurfaceView(false);
                    mRenderToSurfaceViewBtn.setText(R.string.start_record);
                    isStartRecordToSurfaceView = false;
                } else {
                    mRenderView.setSurfaceHolder(mSurfaceView.getHolder());
                    mRenderView.setIsRenderToSurfaceView(true);
                    mRenderToSurfaceViewBtn.setText(R.string.pause_record);
                    isStartRecordToSurfaceView = true;
                }
            }
        });

        mRenderToMediaCodecBtn = (Button) findViewById(R.id.btn_render_to_mediacodec);
        mRenderToMediaCodecBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isStartRecordToMediaCodec) {
                    mRenderView.setIsRenderToMediaCodec(false);
                    mEncoder.stopRecord();
                    isStartRecordToMediaCodec = false;
                    mRenderToMediaCodecBtn.setText(R.string.start_record);
                    gotoViewMovie();
                } else {
                    startRecord();
                    isStartRecordToMediaCodec = true;
                    mRenderToMediaCodecBtn.setText(R.string.pause_record);
                }
            }
        });
    }

    protected void initEncoder(){
        mEncoder = new ViewEncoder();
    }

    protected void initDefaultArgs(){
        mFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "render_to_mediacodec.mp4";
    }

    protected void startRecord() {
        try {
            mEncoder.prepareEncoder(mRenderView.getWidth(), mRenderView.getHeight(), mFilePath);
            mRenderView.setSurface(mEncoder.getInputeSurface());
            mRenderView.setViewEncoder(mEncoder);
            mRenderView.setIsRenderToMediaCodec(true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void gotoViewMovie(){
        Intent intent = new Intent(Intent.ACTION_VIEW);
        String type = "video/mp4";
        Uri uri = Uri.parse("file://" + mFilePath);
        intent.setDataAndType(uri, type);
        startActivity(intent);
    }


    /**
     * getDrawingCache方式
     */
    private void getDrawingCache() {
        TextView view = new TextView(this);
        view.setDrawingCacheEnabled(true);
        Bitmap bitmap = view.getDrawingCache();
    }

    @Override
    protected void onResume() {
        Log.d("rendViewToOpenGL", "onResume");
        super.onResume();
        mHandler.sendEmptyMessageDelayed(MSG_CHANGE, 1000);
    }

    private int count;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_CHANGE:
                    mRenderView.setText("haha" + count++);
                    removeMessages(MSG_CHANGE);
                    sendEmptyMessageDelayed(MSG_CHANGE, 1000);
                    break;
            }
        }
    };
}
